// 内容加载完成监听
document.addEventListener('DOMContentLoaded', function(){
	// 请求路径
	var pathname = window.location.pathname

	switch(pathname) {
		case "/fire/student/list.html":
			// var status = getParamString('status')
			addPrintButton();
			addDeleteStudent();
			addAllStudent();
			addUploadPic();
			break;
		default:
			console.log('未知url');
			break;
	}
});

/**
 * 添加删除学生按钮
 */
function addDeleteStudent() {
	let xblock = document.getElementsByTagName('xblock')[0];
	// 在第一个元素前添加
	if(xblock.children[0]){
		let label = document.createElement('label');
		label.setAttribute('for', 'delStudentAll');
		label.setAttribute('class', 'button');
		label.style.backgroundColor = 'red';
		label.style.height = '40px';
		label.style.padding = '9px 15px';
		label.style.color = 'white';
		label.innerText='批量删除'
		label.style.marginRight = '20px'

		let button = document.createElement('button');
		button.setAttribute('id', 'delStudentAll')
		button.setAttribute('type','button');
		button.setAttribute("onclick", "delStudentBybatch()")
		button.innerText='批量删除'
		button.style.display = 'none';

		xblock.insertBefore(button, xblock.children[0]);
		xblock.insertBefore(label, xblock.children[0]);
	}
}

// 根据excel添加全部学生
function addAllStudent() {
	let xblock = document.getElementsByTagName('xblock')[0];
	// 在第一个元素前添加
	if(xblock.children[0]){
		// var div = document.createElement('div');
		// div.style.width = '50px';
		let label = document.createElement('label');
		label.setAttribute('for', 'inputFile');
		label.setAttribute('class', 'button');
		label.style.backgroundColor = 'blue';
		label.style.height = '40px';
		label.style.padding = '9px 15px';
		label.style.color = 'white';
		label.style.marginRight = '20px'

		let span = document.createElement('span');
		span.innerHTML = '选择文件';
		label.appendChild(span);
		let input = document.createElement('input');
		input.setAttribute('type','file');
		input.setAttribute('id','inputFile');
		input.setAttribute('onchange','fileChanged()');
		input.style.display = 'none';

		xblock.insertBefore(input, xblock.children[0]);
		xblock.insertBefore(label, xblock.children[0]);
		// 添加excel操作工具
		let temp = document.createElement('script');
		temp.setAttribute('type', 'text/javascript');
		temp.src = 'https://cdn.bootcdn.net/ajax/libs/xlsx/0.16.4/xlsx.core.min.js';
		document.body.appendChild(temp);
		injectJs('js/inject_add_student.js');
　　}
}

// 添加打印按钮
function addPrintButton() {
	// 获取表格对象
 	var trPrints = document.getElementsByTagName('tr');
 	// console.log(trPrints);
 	// 遍历
  	for(i = 0, len = trPrints.length; i < len; i++) {
  		var item = trPrints[i];
  		// 为0是表头
    	if (i === 0) {
  			var th = document.createElement("th");
			th.innerHTML = "打印";
		  	item.appendChild(th);
		} else {
			// 添加单元格，打印按钮
			var td = document.createElement('td');
			var button = document.createElement('button');
			button.setAttribute("type", "button");
			button.setAttribute("value", '打印');
			button.setAttribute("id", 'btn_print-' + i);
			button.style.width='50px';
			button.style.height='20px';
			button.innerHTML = '打印';
			button.style.background = 'green';
			button.style.borderColor = 'transparent';
			button.style.color = 'white';
			button.setAttribute("onclick", 'startPrint(this.id)');
			td.appendChild(button);
			item.appendChild(td);
		}
	}

	injectJs('js/inject_print.js');
}

// 添加上传图片按钮
function addUploadPic() {
	// 获取表格对象
 	var trPrints = document.getElementsByTagName('tr');
 	// console.log(trPrints);
 	// 遍历
  	for(i = 0, len = trPrints.length; i < len; i++) {
  		var item = trPrints[i];
  		// 为0是表头
    	if (i == 0) {
  			var th = document.createElement("th");
			th.innerHTML = "上传图片";
		  	item.appendChild(th);
		} else {
			// 添加单元格，上传按钮
			var td = document.createElement('td');
			var label = document.createElement('label');
			label.setAttribute('for', 'inputImg-' + i);
			label.setAttribute('class', 'button');
			label.style.backgroundColor = 'red';
			label.style.height = '20px';
			label.style.padding = '5px 10px';
			label.style.color = 'white';

			var span = document.createElement('span');
			span.innerHTML = '上传';
			label.appendChild(span);
			var input = document.createElement('input');
			input.setAttribute('type','file');
			input.setAttribute('id','inputImg-' + i);
			input.setAttribute('onchange','uploadImg(this.id)');
			input.style.display = 'none';

			td.appendChild(label);
			td.appendChild(input);
			item.appendChild(td);
		}
	}
	injectJs('js/inject_upload_img.js');
}


// 向页面注入JS
// 需要在manifest.json中配置web_accessible_resources
function injectJs(jsPath) {
	var temp = document.createElement('script');
	temp.setAttribute('type', 'text/javascript');
	// 获得的地址类似：chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject_print.js
	temp.src = chrome.extension.getURL(jsPath);
	document.body.appendChild(temp);
}

// 获取请求参数
function getParamString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}
